import 'dart:math';

import 'package:fate_blind_box/utils/page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/picker.dart';

import '../../../network/Api.dart';
import '../../../theme/theme_color.dart';
import '../../../utils/loading.dart';
import '../../../utils/picker.dart';
import '../../../utils/provider.dart';
import '../../../utils/snack_bar.dart';

class PersonInformationThreePage extends StatefulWidget {
  const PersonInformationThreePage({Key? key}) : super(key: key);

  @override
  State<PersonInformationThreePage> createState() => _PersonInformationThreePageState();
}

class _PersonInformationThreePageState extends State<PersonInformationThreePage> {

  late BuildContext buildContext;
  var age = '';
  var tall = '';
  var weight = '';
  var education = '';
  var money = '';
  var house = '';
  var car = '';

  Map<String, dynamic> param = {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getMemberInformation();
  }

  void _getMemberInformation() async{
    final infomation = await Api.getInstance().getMemberInfo(Provider.memberId);
    param.addAll(infomation['data']['mcs']);
  }

  @override
  Widget build(BuildContext context) {
    var _width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: Column(
          children: [
            const Text(
              '择偶要求3/3',
              style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: Color(0xFF2C2C2C)
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 4),
              child: Image.asset(
                'images/person_progress_three3.png',
                width: 88,
                height: 4,
              ),
            )
          ],
        ),
        elevation: 0,
      ),
      backgroundColor: Colors.white,
      body: Builder(builder: (ctx){
        buildContext = ctx;
        return Container(
            width: _width,
            margin: const EdgeInsets.only(top: 10),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  _buildItem('images/person_age.png', '年龄', age, '年龄'),
                  _buildItem('images/person_tall.png', '身高', tall, '身高'),
                  _buildItem('images/person_weight.png', '体重', weight, '体重'),
                  _buildItem('images/person_education.png', '学历', education, '学历'),
                  _buildItem('images/person_getmoney.png', '年收入',  money, '年收入'),
                  _buildItem('images/person_house.png', '购房', house, '购房'),
                  _buildItem('images/person_car.png', '购车', car, '购车'),
                  GestureDetector(
                    onTap: () {
                      _submit();
                    },
                    child: Container(
                      height: 45,
                      margin: const EdgeInsets.fromLTRB(73, 37, 76, 0),
                      decoration: BoxDecoration(
                          color: const Color(0xFF7F69D9),
                          borderRadius: BorderRadius.circular(22.5)
                      ),
                      alignment: Alignment.center,
                      child: const Text("下一步", style: TextStyle(color: Colors.white, fontSize: 18),),
                    ),
                  ),
                  const SizedBox(height: 20,)
                ],
              ),
            )
        );
      })
    );
  }

  void _submit() {
    if(!_verity()){
      return;
    }

    loading(() async {
      final res = await Api.getInstance().modifyMcs(param);
      if (res['code'] == 0) {
        Navigate.toMain();
      } else {
        toast(res['msg']);
      }
    });

  }

  bool _verity() {
    if(age.isEmpty){
      toast('请选择年龄');
      return false;
    }
    if(tall.isEmpty){
      toast('请选择身高');
      return false;
    }
    if(weight.isEmpty){
      toast('请选择体重');
      return false;
    }
    if(education.isEmpty){
      toast('请选择学历');
      return false;
    }

    if(money.isEmpty){
      toast('请选择年收入');
      return false;
    }
    if(house.isEmpty){
      toast('请选择有无住房');
      return false;
    }
    if(car.isEmpty){
      toast('请选择有无车');
      return false;
    }

    return true;
  }

  Widget _buildItem(String icon, String title, String content, String id) {
    return GestureDetector(
      onTap: () {
        _onClick(id);
      },
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 46),
        padding: const EdgeInsets.only(bottom: 5),
        alignment: Alignment.bottomCenter,
        height: 48,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 1,
                    color: ThemeColor.dividerColor
                )
            )
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Image.asset(
                  icon,
                  width: 16,
                  height: 16,
                ),
                const SizedBox(width: 6,),
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: Color(0xFF1B1F2F),
                  ),
                ),
              ],
            ),
            Row(
              children: [
                Text(
                  content.isEmpty ? '点击选择' : content,
                  style: TextStyle(
                      fontSize: 14,
                      color: content.isEmpty ? const Color(0xFF777777) : ThemeColor.appColor
                  ),
                ),
                const SizedBox(width: 5,),
                Image.asset(
                  'images/person_threejiao.png',
                  width: 9,
                  height: 7,
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  Future<void> _onClick(String id) async {
    switch(id) {
      case '年龄':
        showNumberPicker(
            buildContext,
            NumberPickerAdapter(data: [
              const NumberPickerColumn(begin: 18, end: 50),
              const NumberPickerColumn(begin: 18, end: 50),
            ]),
                (picker, value) {
              final vMin = 18 + min(value[0], value[1]);
              final vMax = 18 + max(value[0], value[1]);
              setState(() {
                param['ageStart'] = vMin.toInt();
                param['ageEnd'] = vMax.toInt();
                age = '${vMin.toString()}~${vMax.toString()}';
              });
            });
        break;
      case '身高':
        showNumberPicker(
            buildContext,
            NumberPickerAdapter(data: [
              const NumberPickerColumn(begin: 150, end: 200),
              const NumberPickerColumn(begin: 150, end: 200),
            ]),
                (picker, value) {
              final vMin = 150 + min(value[0], value[1]);
              final vMax = 150 + max(value[0], value[1]);
              setState(() {
                param['heightStart'] = vMin.toInt();
                param['heightEnd'] = vMax.toInt();
                tall = '${vMin.toString()}cm~${vMax.toString()}cm';
              });
            });
        break;
      case '体重':
        showNumberPicker(
            buildContext,
            NumberPickerAdapter(data: [
              const NumberPickerColumn(begin: 40, end: 200),
              const NumberPickerColumn(begin: 40, end: 200),
            ]),
                (picker, value) {
              final vMin = 40 + min(value[0], value[1]);
              final vMax = 40 + max(value[0], value[1]);
              setState(() {
                param['weightStart'] = vMin.toInt();
                param['weightEnd'] = vMax.toInt();
                weight = '${vMin.toString()}kg~${vMax.toString()}kg';
              });
            });
        break;
      case '学历':
        showPicker(buildContext, ['小学', '初中', '高中', '大专', '本科', '硕士', '博士', '教授'], (picker, value) {
          setState(() {
            education = picker.getSelectedValues()[0].toString();
            param['edu'] = value[0];
          });
        });
        break;
      case '年收入':
        showPicker(buildContext, ['5万~10万', '10万~20万', '20万~50万', '50万~100万', '100万~300万', '300万以上'], (picker, value) {
          setState(() {
            money = picker.getSelectedValues()[0].toString();
            param['incomeVal'] = value[0];
          });
        });
        break;
      case '购房':
        showPicker(buildContext, ['无', '有'], (picker, value) {
          setState(() {
            house = picker.getSelectedValues()[0].toString();
            param['purchase'] = value[0];
          });
        });
        break;
      case '购车':
        showPicker(buildContext, ['无', '有'], (picker, value) {
          setState(() {
            car = picker.getSelectedValues()[0].toString();
            param['buyCar'] = value[0];
          });
        });
        break;
      default: break;
    }
  }
}
